package com.estate.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.Activity;
import com.estate.bean.Repair;
import com.estate.mapper.RepairMapper;
import com.estate.service.feign.BuildingFeign;
import com.estate.service.feign.DeviceFeign;
import com.estate.service.feign.OwnerFeign;
import com.estate.service.feign.QuartersFeign;
import com.estate.service.service.RepairService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    @Resource
    private QuartersFeign quartersFeign;
    @Resource
    private OwnerFeign ownerFeign;
    @Resource
    private BuildingFeign buildingFeign;
    @Resource
    private DeviceFeign deviceFeign;

    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Repair> list = list();
        list.forEach(e->{
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
            e.setOwner(ownerFeign.find(e.getOwnerId()));
            e.setBuilding(buildingFeign.find(e.getBuildingId()));
            e.setDevice(deviceFeign.find(e.getDeviceId()));
        });
        PageInfo<Repair> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult saveRepair(Repair repair) {
        repair.setStatus(0);
        repair.setIsDelete(0);
        log.info("新增repair数据:{}", repair);
        boolean b = save(repair);
        if (!b){
            return ResultTool.fail(501,"添加报修成功");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult deleteRepair(String repairId) {
        log.info("删除的repairId:{}", repairId);
        boolean b = removeById(repairId);
        if (!b){
            return ResultTool.fail(502,"删除报修成功");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult updateRepair(Repair repair) {
        log.info("修改repair数据:{}", repair);
        boolean b = updateById(repair);
        if (!b) {
            return ResultTool.fail(503, "修改报修失败");
        }
        return ResultTool.success("success");
    }

}
